<template>
	<view class="app">
		<button @click="run" v-if="state == 0">同时测试事务和非事务</button>
		<button @click="stop" v-if="state != 0">停止测稳定性</button>
		<view style="font-weight: bold;">每次2次数据库操作</view>
		<view style="display: flex;margin-top: 20px;">
			<view>
				无事务的测试情况
				<view></view>
				<view>总请求 {{ db1Num() }} 次</view>
				<view>
					请求成功 {{ db1.successNum }} 次
					<text v-if="db1.successNum || db1.failNum">
						({{ parseFloat(((db1.successNum / db1Num()) * 100).toFixed(2)) }}%)
					</text>
				</view>
				<view>
					请求失败 {{ db1.failNum }} 次
					<text v-if="db1.successNum || db1.failNum">
						({{ parseFloat(((db1.failNum / db1Num()) * 100).toFixed(2)) }}%)
					</text>
				</view>
				<view>请求成功平均耗时 {{ db1.avgTime }} 毫秒</view>
			</view>

			<view style="margin-left: 10px;">
				有事务的测试情况
				<view></view>
				<view>总请求 {{ db2Num() }} 次</view>
				<view>
					请求成功 {{ db2.successNum }} 次
					<text v-if="db2.successNum || db2.failNum">
						({{ parseFloat(((db2.successNum / db2Num()) * 100).toFixed(2)) }}%)
					</text>
				</view>
				<view>
					请求失败 {{ db2.failNum }} 次
					<text v-if="db2.successNum || db2.failNum">
						({{ parseFloat(((db2.failNum / db2Num()) * 100).toFixed(2)) }}%)
					</text>
				</view>
				<view>请求成功平均耗时 {{ db2.avgTime }} 毫秒</view>
			</view>
		</view>
	</view>
</template>

<script>
var runKey;
export default {
	data() {
		return {
			state: 0,
			db1: {
				allTime: 0,
				avgTime: 0,
				successNum: 0,
				failNum: 0,
				runNum: 0
			},
			db2: {
				allTime: 0,
				avgTime: 0,
				successNum: 0,
				failNum: 0,
				runNum: 0
			}
		};
	},
	onLoad(options) {
	},
	methods: {
		run() {
			this.state = 1;
			this.db1.successNum = 0;
			this.db1.failNum = 0;
			this.db1.allTime = 0;
			this.db1.avgTime = 0;
			this.db1.runNum = 0;
			
			this.db2.successNum = 0;
			this.db2.failNum = 0;
			this.db2.allTime = 0;
			this.db2.avgTime = 0;
			this.db2.runNum = 0;
			this.run1();
			this.run2();
		},
		// 无事务
		run1(){
			let that = this;
			if (that.state === 0) {
				return;
			}
			that.db1.runNum++;
			let time1 = Date.now();
			uniCloud.callFunction({
				name: "test-stability-B",
				data: {
					action: "db-2"
				},
				success(data) {
					that.db1.successNum++;
					let time2 = Date.now();
					that.db1.allTime += time2 - time1;
					that.db1.avgTime = parseInt(that.db1.allTime / that.db1.successNum);
				},
				fail(err) {
					console.log("fail1", err);
					that.db1.failNum++;
				},
				complete() {
					setTimeout(()=>{
						that.run1();
					},100); 
				}
			});
		},
		// 有事务
		run2(){
			let that = this;
			if (that.state === 0) {
				return;
			}
			that.db2.runNum++;
			let time1 = Date.now();
			uniCloud.callFunction({
				name: "test-stability-B",
				data: {
					action: "db-transaction-2"
				},
				success(data) {
					that.db2.successNum++;
					let time2 = Date.now();
					that.db2.allTime += time2 - time1;
					that.db2.avgTime = parseInt(that.db2.allTime / that.db2.successNum);
				},
				fail(err) {
					console.log("fail2", err);
					that.db2.failNum++;
				},
				complete() {
					setTimeout(()=>{
						that.run2();
					},100); 
				}
			});
		},
		stop() {
			this.state = 0;
		},
		db1Num() {
			return this.db1.successNum + this.db1.failNum;
		},
		db2Num() {
			return this.db2.successNum + this.db2.failNum;
		}
	}
};
</script>

<style>
.app {
	padding: 15px;
	font-size: 13px;
}
.app input {
	height: 46px;
	border: solid 1px #dddddd;
	border-radius: 5px;
	margin-bottom: 15px;
	padding: 0px 15px;
}
.app button {
	margin-bottom: 15px;
	font-size: 32rpx;
	margin-left: auto;
	margin-right: auto;
	padding-left: 14px;
	padding-right: 14px;
}
.app navigator {
	display: inline-block;
	color: #007aff;
	border-bottom: solid 1px #007aff;
	font-size: 16px;
	line-height: 24px;
	margin-bottom: 15px;
}
</style>
